#!/bin/bash
# Script to find the equilibrium lattice constant of bcc Fe

# Get the number of processors, by default, 1
if [ "$#" -ge 1 ]; then
   np=$1
else
   np=4
fi

# Define the output file and its header
outfile='eng_a.dat'
echo '#vol/atom energy(eV/cell) a(ang) mag(mu) time-used(seconds)' > ${outfile}

# Loop over a
for a in `seq 2.70 0.02 2.96`
do
# Prepare POSCAR
cat > POSCAR << EOF
BCC Fe - primitive cell
${a}
0.5 0.5 -0.5
-0.5 0.5 0.5
0.5 -0.5 0.5
Fe
1
Direct
0.0 0.0 0.0
EOF

# Run vasp
mpirun --allow-run-as-root -np ${np} vasp

# Collect info
nat=`grep 'NIONS' OUTCAR |tail -1|awk '{print $12}'`
vol=`grep ' volume of cell' OUTCAR |tail -1|awk -v n=${nat} '{print $5/n}'`
eng=`grep 'free  energy' OUTCAR | awk -v n=${nat} '{print $5/n}'`
mag=`grep 'mag=' OSZICAR |awk '{print $10}'`
sec=`grep 'Total CPU time used' OUTCAR |awk '{print $6}'`
echo "$vol $eng $a $mag $sec" >> ${outfile}

rm -rf WAVECAR

done

# Clean up
rm -rf CHG CHGCAR CONTCAR DOSCAR EIGENVAL IBZKPT OSZICAR OUTCAR PBS.batch PCDAT vasprun.xml XDATCAR REPORT
